package com.mychat.enums;

import com.mychat.utils.StringUtils;

public enum UserContactTypeEnum {
    USER(0, "U", "好友"),
    GROUP(1, "G", "群组");
    private Integer type;
    private String prefix;
    private String desc;

    UserContactTypeEnum(Integer type, String prefix, String desc) {
        this.type = type;
        this.prefix = prefix;
        this.desc = desc;
    }

    public static UserContactTypeEnum getByPrefix(String contactId) {
        if (StringUtils.isEmpty(contactId)) {
            return null;
        }
        switch (contactId.charAt(0)) {
            case 'U':
                return USER;
            case 'G':
                return GROUP;
            default:
                return null;
        }
    }

    public Integer getType() {
        return type;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getDesc() {
        return desc;
    }

    public static UserContactTypeEnum getEnumByType(Integer type) {
        for (UserContactTypeEnum item : UserContactTypeEnum.values()) {
            if (item.getType().equals(type)) {
                return item;
            }
        }
        return null;
    }

    public static UserContactTypeEnum getEnumByPrefix(String prefix) {
        for (UserContactTypeEnum item : UserContactTypeEnum.values()) {
            if (item.getPrefix().equals(prefix)) {
                return item;
            }
        }
        return null;
    }

    public static UserContactTypeEnum getEnumByDesc(String desc) {
        for (UserContactTypeEnum item : UserContactTypeEnum.values()) {
            if (item.getDesc().equals(desc)) {
                return item;
            }
        }
        return null;
    }

    public static  UserContactTypeEnum getEnumByName(String name) {
        try{
            if(StringUtils.isEmpty(name)) {
                return null;
            }
            return UserContactTypeEnum.valueOf(name.toUpperCase());
        } catch(Exception e) {
            return null;
        }
    }
}
